﻿using System;
using LayerEntity.Persona ;
using LayerEntity.Sistema;
using LayerLogic;

namespace System2012.Persona.UserControls
{
    public partial class UCDireccion : DevExpress.XtraEditors.XtraUserControl
    {
        #region Variables
        EnumOperaciones intOperacion = EnumOperaciones.Insertar ;
        string idPersona = "";
        int idDireccion = 0;
        bool isPrincipal = false;
        internal delegate void UpdateFocus();

        internal event UpdateFocus CambiarFoco =delegate {};

        #endregion

        #region Propiedades
        public EnumOperaciones Operacion
        {
            get
            {
                return this.intOperacion;
            }
        }
        #endregion
        public UCDireccion()
        {
            this.InitializeComponent();
        }

        private void ClickAndSearch(object sender, EventArgs e)
        {
            System2012.Sistema.Ubigeos.BuscarUbigeo b = new System2012.Sistema.Ubigeos.BuscarUbigeo();
            b.ShowDialog();
            if (b.StateSelection)
            {
                this.BTBuscarUbigeo.EditValue = b.UbigeoSelected.ToString();
                this.TxtUbigeo.EditValue = b.UbigeoSelected.Ubigeo;
            }
        }

        #region Metodos
        internal void SetDireccion(EDireccion addressInfo)
        {
            if (addressInfo.Ubigeo.Trim() == string.Empty)
            {
                this.NewAddress();
            }
            else
            {
                this.idDireccion = addressInfo.Item;
                this.intOperacion = EnumOperaciones.Actualizar;
                this.TxtDireccion.EditValue = addressInfo.Direccion;
                this.TxtContacto.EditValue = addressInfo.NombreContacto;
                this.TxtEmail.EditValue = addressInfo.EMail;
                this.TxtMovil.EditValue = addressInfo.Movil;
                this.TxtNextel.EditValue = addressInfo.Nextel;
                this.TxtReferencia.EditValue = addressInfo.Referencia;
                this.TxtRpm.EditValue = addressInfo.Rpm;
                this.TxtTelefono.EditValue = addressInfo.Telefono;
                this.idPersona = addressInfo.IdPersona;
                this.TxtUbigeo.EditValue = addressInfo.Ubigeo;
                this.isPrincipal = addressInfo.IsPrincipal;  
                this.BTBuscarUbigeo.EditValue = SystemUtils.RecordManagerCommonList.GetUbigeoByUbigeo(addressInfo.Ubigeo).ToString();
            }
        }

        internal void NewAddress()
        {
            this.NewAddress("");
        }

        internal void NewAddress(string persona)
        {
            this.intOperacion = EnumOperaciones.Insertar ;
            this.TxtContacto.EditValue = string.Empty;
            this.TxtDireccion.EditValue = string.Empty;
            this.TxtEmail.EditValue = string.Empty;
            this.TxtMovil.EditValue = string.Empty;
            this.TxtNextel.EditValue = string.Empty;
            this.TxtReferencia.EditValue = string.Empty;
            this.TxtRpm.EditValue = string.Empty;
            this.TxtTelefono.EditValue = string.Empty;
            this.TxtUbigeo.EditValue = string.Empty;
            this.idPersona = persona;
            this.BTBuscarUbigeo.EditValue = string.Empty;
            this.idDireccion = 0;
        }

        internal void SetFocus(bool back) {
            if (!back)
            {
                TxtDireccion.Focus();
            }
            else {
                TxtReferencia.Focus();
            }
        }

        #endregion

        #region Funciones
        internal EDireccion GetDireccion()
        {
            EDireccion dir = null;
            if (this.TxtDireccion.EditValue.ToString().Trim() != string.Empty)
            {
                if (this.BTBuscarUbigeo.EditValue.ToString().Trim() != string.Empty)
                {
                    dir = new EDireccion();
                    if (this.Operacion == EnumOperaciones.Actualizar)
                    {
                        dir.Item = this.idDireccion;
                    }
                    else
                    {
                        dir.Item = 0;
                    }
                    dir.NombreContacto = this.TxtContacto.EditValue.ToString();
                    dir.Direccion = this.TxtDireccion.EditValue.ToString();
                    dir.EMail = this.TxtEmail.EditValue.ToString();
                    dir.IdEstado = 1;
                    dir.IdPersona = this.idPersona;
                    dir.Movil = this.TxtMovil.EditValue.ToString();
                    dir.Nextel = this.TxtNextel.EditValue.ToString();
                    dir.Referencia = this.TxtReferencia.EditValue.ToString();
                    dir.Rpm = this.TxtRpm.EditValue.ToString();
                    dir.Telefono = this.TxtTelefono.EditValue.ToString();
                    dir.Ubigeo = this.TxtUbigeo.EditValue.ToString();
                    dir.IsPrincipal = this.isPrincipal;
                }
                else
                {
                    SystemUtils.AlerMessaje("Seleccione un Ubigeo");
                    this.TxtUbigeo.Focus();
                }
            }
            else
            {
                SystemUtils.AlerMessaje("Ingrese una Direccion");
                this.TxtDireccion.Focus();
            }

            return dir;
        }

        #endregion

        private void TxtUbigeo_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Enter || e.KeyCode ==System.Windows.Forms.Keys.Right || e.KeyCode ==System.Windows.Forms.Keys.Down )
            {
                if (sender == TxtUbigeo)
                {
                    if (this.TxtUbigeo.EditValue.ToString().Length == 6)
                    {
                        EUbigeo ubigeo = SystemUtils.RecordManagerCommonList.GetUbigeoByUbigeo(this.TxtUbigeo.EditValue.ToString());
                        if (ubigeo != null)
                        {
                            this.BTBuscarUbigeo.EditValue = ubigeo.ToString();
                        }
                    }
                    System.Windows.Forms.SendKeys.Send("{TAB}");
                }
                else {
                    System.Windows.Forms.SendKeys.Send("{TAB}");
                }
            }
            if (e.KeyCode == System.Windows.Forms.Keys.Up ) {
                if (sender == TxtDireccion)
                {
                    CambiarFoco();
                }
                else
                {
                    SelectNextControl((System.Windows.Forms.Control)sender, false, true, true, true);
                }
            }
        }
    }
}